package com.zhenwgang.listen_books.servlet.album;

import com.zhenwgang.listen_books.repository.AlbumRepo;
import com.zhenwgang.listen_books.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Violet
 * @date 2022/07/14 19:03
 **/
@WebServlet("/studio/album/create.do")
public class CreateDOServlet extends HttpServlet {
    private final AlbumRepo albumRepo = new AlbumRepo();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null){
            throw new RuntimeException("未登录");
        }
        UserVO currentUser = (UserVO) session.getAttribute("currentUser");
        if (currentUser == null){
            throw new RuntimeException("未登录");
        }

        req.setCharacterEncoding("utf-8");
        int uid = currentUser.uid;
        String title = req.getParameter("title");
        String cover = req.getParameter("cover");
        int state = 1;
        albumRepo.insert(uid, title, cover, state);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        resp.getWriter().println("专辑创建成功");
    }
}